package utility;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 *
 * @author alfian
 */
public class Zip {
    public Zip(){
        
    }
    public void compress(String absolutePath){
        byte[] buffer = new byte[1024];
        String extension;
          int dotPos = absolutePath.lastIndexOf(".");
          int slash = absolutePath.lastIndexOf("/");
          extension = absolutePath.substring(dotPos);
          String fileName = absolutePath.substring(slash+1, dotPos);
          String fullName = absolutePath.substring(slash+1, absolutePath.length());
          String url= absolutePath.substring(0,slash+1);
    //          System.out.println(extension);
    //          System.out.println(fileName);
    //          System.out.println(url);
    //          System.out.println(fullName);
            try{
                FileOutputStream fos = new FileOutputStream(url+fileName+".zip");
                //System.out.println("fos="+url+fileName+".zip");
                ZipOutputStream zos = new ZipOutputStream(fos);
                ZipEntry ze= new ZipEntry(fullName);
                //System.out.println("fullname="+fullName);
                zos.putNextEntry(ze);
                FileInputStream in = new FileInputStream(absolutePath);
                //System.out.println("absolutepath="+absolutePath);
                int len;
                while ((len = in.read(buffer)) > 0) {
                        zos.write(buffer, 0, len);
                }
                in.close();
                zos.closeEntry();
                //remember close it
                zos.close();
            }catch(IOException ex){
               ex.printStackTrace();
            }        
    }
    public String compressGetFileName(String absolutePath){
        String result="";
        compress(absolutePath);
        String extension;
          int dotPos = absolutePath.lastIndexOf(".");
          int slash = absolutePath.lastIndexOf("/");
          extension = absolutePath.substring(dotPos);
          String fileName = absolutePath.substring(slash+1, dotPos);
          String fullName = absolutePath.substring(slash+1, absolutePath.length());
          String url= absolutePath.substring(0,slash+1);
        //result+=;
        return result;
    }
    public void uncompress(String absolutePath){
        String zipName = absolutePath;
        String extension;
          int dotPos = absolutePath.lastIndexOf(".");
          int slash = absolutePath.lastIndexOf("/");
          extension = absolutePath.substring(dotPos);
          String fileName = absolutePath.substring(slash+1, dotPos);
          String fullName = absolutePath.substring(slash+1, absolutePath.length());
          String url= absolutePath.substring(0,slash+1);

        try {
            FileInputStream fis = new FileInputStream(zipName);
            ZipInputStream zis = new ZipInputStream(
                    new BufferedInputStream(fis));
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                System.out.println("Unzipping: " + entry.getName());

                int size;
                byte[] buffer = new byte[2048];

                FileOutputStream fos =
                        new FileOutputStream(url+entry.getName());
                BufferedOutputStream bos =
                        new BufferedOutputStream(fos, buffer.length);

                while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
                    bos.write(buffer, 0, size);
                }
                bos.flush();
                bos.close();
            }

            zis.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {       
        Zip zip = new Zip();
        //zip.compress("D:/Toefl/Barron_GRE.pdf");
        //zip.uncompress("D:/Toefl/Barron_GRE.zip");
    }
}
